package cn.tedu.ivos.user.controller;

import cn.tedu.ivos.base.response.JsonResult;
import cn.tedu.ivos.user.service.UserService;
import cn.tedu.ivos.user.pojo.dto.UserLoginParam;
import cn.tedu.ivos.user.pojo.dto.UserQueryParam;
import cn.tedu.ivos.user.pojo.dto.UserSaveParam;
import cn.tedu.ivos.user.pojo.vo.UserVO;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Tag(name = "用户模块")
@Slf4j
@RestController
@RequestMapping("/v1/user/")
public class UserController {
    @Autowired
    UserService userService;

    @PostMapping("login")
    @Operation(summary = "用户登录功能")
    @ApiOperationSupport(order = 10)
    public JsonResult login(@Validated @RequestBody UserLoginParam userLoginParam) {
        log.debug("用户登录：UserLoginParam:{}", userLoginParam);
        UserVO userVO = userService.login(userLoginParam);
        return JsonResult.ok(userVO);
    }

    @GetMapping("select")
    @ApiOperationSupport(order = 20)
    @Operation(summary = "查询用户")
    public JsonResult selectUser(UserQueryParam userQueryParam) {
        log.debug("userQueryParam:{}", userQueryParam);
        List<UserVO> list = userService.selectUser(userQueryParam);
        return JsonResult.ok(list);
    }

    @PostMapping("save")
    @ApiOperationSupport(order = 30)
    @Operation(summary = "新建用户")
    public JsonResult saveUser(@Validated UserSaveParam userSaveParam) {
        userService.saveUser(userSaveParam);
        return JsonResult.ok();
    }

    @PostMapping("update/password/{userId}")
    @Operation(summary = "重置密码")
    @ApiOperationSupport(order = 40)
    public JsonResult resetPassword(@PathVariable Long userId) {
        userService.resetPassword(userId);
        return JsonResult.ok();
    }

    @PostMapping("update/status/{userId}/{status}")
    @Operation(summary = "修改用户状态")
    @ApiOperationSupport(order = 50)
    public JsonResult changeStatus(@PathVariable Long userId, @PathVariable String status) {
        userService.changeStatus(userId, status);
        return JsonResult.ok();
    }

    @PostMapping("delete/{id}")
    @Operation(summary = "删除用户")
    @ApiOperationSupport(order = 60)
    public JsonResult deleteUser(@PathVariable Long id) {
        userService.deleteUser(id);
        return JsonResult.ok();
    }

    @GetMapping("select/audit/{parentId}")
    @Operation(summary = "查询用户待审核列表")
    @ApiOperationSupport(order = 70)
    public JsonResult selectAuditUser(@PathVariable Long parentId) {
        log.debug("查询用户待审核列表上级 parentId{}", parentId);
        List<UserVO> list = userService.selectAuditUser(parentId);
        return JsonResult.ok(list);
    }
}
